package com.anjiplus.template.gaea.business.modules.quartz.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import org.springframework.http.HttpStatus;

@Data
public class Result {

    //返回信息
    private String msg;
    //返回标示
    private int code;
    //返回数据
    private Object data;
    //每页条数
    private long pageSize;
    //当前页数
    private long pageIndex;
    //总条数
    private long total;


    public static Result success(){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        return result;
    }

    public static Result fail(){
        Result result = new Result();
        result.setCode(HttpStatus.METHOD_FAILURE.value());
        result.setMsg(HttpStatus.METHOD_FAILURE.getReasonPhrase());
        return result;
    }

    public static Result success(String msg){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(StrUtil.isNotEmpty(msg)?msg:HttpStatus.OK.getReasonPhrase());
        return result;
    }

    public static Result success(String msg , Object returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(StrUtil.isNotEmpty(msg)?msg:HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }


    public static Result success(Integer code , Object returnObj){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    public static Result success(Object returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }
    public static Result convertIpageToSuccess(IPage iPage){
        if(null == iPage){
            return Result.success();
        }
        Result result = new Result();
        result.setData(iPage.getRecords());
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setPageSize(iPage.getSize());
        result.setPageIndex(iPage.getCurrent());
        result.setTotal(iPage.getTotal());
        return result;
    }

    public static Result successString(String returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    public static Result success(Object returnObj,long total){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        result.setTotal(total);
        return result;
    }

    public static Result successtotal(Object returnObj,long total){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        result.setTotal(total);
        return result;
    }

    public static Result successForQuery(Object returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        processResult(result);
        return result;
    }

    public static Result successForQuery(String msg , Object returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(StrUtil.isNotEmpty(msg)?msg:HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        processResult(result);
        return result;
    }

    public static Result successForQuery(Object returnObj,long total){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        result.setTotal(total);
        return result;
    }


    public static Result fail(String msg){
        Result result = new Result();
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMsg(StrUtil.isNotEmpty(msg)?msg:HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        return result;
    }

    public static Result fail(int code , String msg , Object returnObj){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(StrUtil.isNotEmpty(msg)?msg:HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    public static Result fail(String msg , Object returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMsg(StrUtil.isNotEmpty(msg)?msg:HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    private static  void processResult(Result result){
        if (ObjectUtil.isEmpty(result.getData())) {
            result.setCode(706);
            result.setMsg("暂无数据！");
        }
    }

    // 无token返回502无登录状态
    public static  Result noToken(String msg){
        Result result = new Result();
        result.setCode(HttpStatus.BAD_GATEWAY.value());
        result.setMsg(StrUtil.isNotEmpty(msg) ? msg : HttpStatus.BAD_GATEWAY.getReasonPhrase());
        return result;
    }

}
